package it.low.game.client.gui.input;

import it.low.game.client.GameClient;
import it.low.game.client.GameSession;
import it.low.game.client.gui.button.Button;
import it.low.game.client.gui.panel.GameBoardPanel;
import it.low.game.client.util.ClientLogManager;
import it.low.game.core.location.MapPoint;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class InputManager implements ActionListener, MouseListener  {
	
	private static final int NUM_KEY_CODES = 600;
	private InputKey[] inputCommandKeys = new InputKey[NUM_KEY_CODES];
	
	private Component component = null;
	
	public InputManager(Component component){
		this.component = component;		
		this.component.setFocusTraversalKeysEnabled(false);
	}
	
	public void init(){
		ClientLogManager.logMsg("call InputManager.init()");
	}
	
	public void setCursor(Cursor cursor){
		component.setCursor(cursor);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		ClientLogManager.logMsg("call InputManager.actionPerformed()");
		ClientLogManager.logMsg("call InputManager.actionPerformed() actionEvent = "+e.getActionCommand());
		
		Button button = (Button)e.getSource();
		button.pressButton();
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		Point screenPoint = e.getPoint();
		
		GameSession gameSession = GameClient.getGameSession();
		if(GameBoardPanel.isGameViewArea(screenPoint)){
			Point gamePanelPoint = GameBoardPanel.getGamePanelRelativePoint(screenPoint);
			MapPoint mapPoint = gameSession.getMapRelativePoint(gamePanelPoint);
			gameSession.movePlayer(mapPoint);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
			
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseEntered(MouseEvent e) {		
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub		
	}
	
	/*
	@Override
	public void keyTyped(KeyEvent e) {
		e.consume();		
	}

	
	@Override
	public void keyPressed(KeyEvent e) {
		InputCommandKey inputCommandKey = this.getKeyAction(e);
		if(inputCommandKey!=null){
			inputCommandKey.pressInputCommand();
		}
		e.consume();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		InputCommandKey inputCommandKey = this.getKeyAction(e);
		if(inputCommandKey!=null){
			inputCommandKey.relaseInputCommand();
		}
		e.consume();
	}*/
	
	
	public void addBoard(JPanel jPanel){
		jPanel.addMouseListener(this);
	}
	
	public void addButton(Button button){
		InputKeyButton inputKeyButton = button.getInputKeyButton();
		int keyCode = inputKeyButton.getKeyCode();
		if (keyCode < inputCommandKeys.length){
			// Code to check if the key is already assigned
			if(inputCommandKeys[keyCode]!=null){
				ClientLogManager.logWar("the key "+KeyEvent.getKeyText(keyCode));
				ClientLogManager.logWar("is already assigned to "+inputCommandKeys[keyCode].getInput());
			}		
			inputCommandKeys[keyCode] = inputKeyButton;
			button.addActionListener(this);
		}
	}

}
